﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Domain.Entity;

namespace WarehouseManagement.Controllers
{
    /// <summary>
    /// 入库管理器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class WarehouseSAppController : ControllerBase
    {
        private readonly IMediator _mediator;

        public WarehouseSAppController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 入库申请新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddWarehouseSApp(AddWarehouseSAppDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                command.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                command.State = 1;//待入库
                result.Data = await _mediator.Send(new WarehouseSAppCreateCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 入库申请列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WarehouseSAppPage>> GetWarehouseSApp([FromQuery]WarehouseSAppSelectCommand command)
        {
            ApiResult<WarehouseSAppPage> result = new ApiResult<WarehouseSAppPage>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                //command.State = 1;//待入库
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除入库申请 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelWarehouseSApp(WarehouseSAppDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                //获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                //command.State = 1;//待入库
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 入库申请修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdWarehouseSApp(UpdWarehouseSAppDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new WarehouseSAppUpdateCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填入库申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FanWarehouseSAppDto>> FanWarehouseSApp([FromQuery] WarehouseSAppFanCommand command)
        {
            ApiResult<FanWarehouseSAppDto> result = new ApiResult<FanWarehouseSAppDto>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                //command.State = 1;//待入库
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 确认入库申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdCWE(UpdWarehouseSAppDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                
                result.Data = await _mediator.Send(new ConfirmWarehouseentryCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 确认入库列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WarehouseSAppPage>> GetCWE([FromQuery] GetConfirmWarehouseentryCommand command)
        {
            ApiResult<WarehouseSAppPage> result = new ApiResult<WarehouseSAppPage>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                //command.State = 1;//待入库
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 退料的申请入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> StrippingWarehouseSApp(UpdWarehouseSAppDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                command.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new StrippingWarehousingStateCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
     
        /// <summary>
        /// 王思雨专用入库列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ListWarehouseSAppDto>>> ListWarehouseSApps()
        {
            ApiResult<List<ListWarehouseSAppDto>> result = new ApiResult<List<ListWarehouseSAppDto>>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                //command.State = 1;//待入库
                result.Data = await _mediator.Send(new ListWarehouseSAppCommand());
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }














    }
}
